package com.yylending.las.lending;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.utils.FormatUtils;
import com.yylending.las.common.widgets.RedDotView;
import com.yylending.las.common.widgets.TelephoneTextView;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.main.MainController;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 借钱申请提交后的状态:需要现调
 * Created by ying_he on 2017/6/23.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class SiteInvestigationLendingFragment extends BaseFragment {

    static final String ARG_APPLY_STATUS = "arg_apply_status";

    Unbinder unbinder;
    @BindView(R.id.title_tv)
    TextView mTitleTv;
    @BindView(R.id.loan_sum_tv)
    TextView mLoanSumTv;
    @BindView(R.id.service_phone_tv)
    TelephoneTextView mServicePhoneTv;
    @BindView(R.id.lending_notice_tv)
    TextView mLendingNoticeTv;
    @BindView(R.id.title_left_iv)
    ImageView mTitleLeftIv;
    @BindView(R.id.title_right_tv)
    TextView mTitleRightTv;
    @BindView(R.id.title_right_iv)
    ImageView mTitleRightIv;
    @BindView(R.id.layout_title_bar)
    RelativeLayout mLayoutTitleBar;
    @BindView(R.id.loan_btn)
    TextView mLoanBtn;
    @BindView(R.id.notice_phone_divider)
    TextView mNoticePhoneDivider;
    @BindView(R.id.red_dot_view)
    RedDotView mRedDotView;

    public static BaseFragment newInstance(MyQuotaModel.ApplyStatus status) {
        BaseFragment fragment = new SiteInvestigationLendingFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_APPLY_STATUS, status);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_site_investigation_lending, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        StatService.onEvent(getContext(), StatIDSet.EVENT_ID_BREWED_IN_THE_AUDIT);
        return view;
    }

    private void initView() {
        MyQuotaModel.ApplyStatus applyStatus = getArguments().getParcelable(ARG_APPLY_STATUS);
        mTitleTv.setText(R.string.tab_lend);
        mTitleLeftIv.setImageResource(R.drawable.ic_home_help);
        mTitleRightIv.setImageResource(R.drawable.ic_message_white);
        mLoanSumTv.setText(FormatUtils.getDecoratedAmountWithSymbol(applyStatus.loanAmount));
        mServicePhoneTv.setPageNo(Constants.PAGE_NO_MAIN_ACTIVITY);
        MainController.initRedDot(mRedDotView);
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.title_left_iv, R.id.title_right_iv, R.id.lending_notice_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_iv:
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_HELP_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                break;
            case R.id.title_right_iv:
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_MESSAGE_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                MainController.hideRedDot(mRedDotView);
                break;
            case R.id.lending_notice_tv:
                EventAgent.post(new QuotaEvent.ShowLoanNoticeEvent(QuotaEvent.ShowLoanNoticeEvent.QUOTA_SHOW_LOAN_NOTICE, Constants.PAGE_NO_MAIN_ACTIVITY));
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedDotShowEvent(QuotaEvent.ShowRedDotEvent event) {
        mRedDotView.setVisibility(View.VISIBLE);
    }
}
